package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @date 2023/5/18 14:29
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;
    /**
     * 增加菜品,同时保存口味
     * @param dishDto 传输实体类
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //把菜品保存在表中
        this.save(dishDto);
        //获取id
        Long dishId = dishDto.getId();
        //获取里面的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //给每一个口味加上口味表的id
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor>lqw=new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lqw);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品信息
        this.updateById(dishDto);
        //清理口味内容
        LambdaQueryWrapper<DishFlavor>lqw=new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);
        //存放新的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //给每一个口味加上口味表的id
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void removeWithFlavor(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            this.removeById(ids[i]);
            //清理口味内容
            LambdaQueryWrapper<DishFlavor>lqw=new LambdaQueryWrapper<>();
            lqw.eq(DishFlavor::getDishId,ids[i]);
            dishFlavorService.remove(lqw);
        }
    }

    /**
     * TODO 查询删除的菜品是否在套餐里面
     * @param ids
     */
    @Override
    public void query(Long[] ids) {

    }

    /**
     * 修改菜品状态
     * TODO 查询停售的菜品是否在套餐里面
     * @param ids
     * @param id
     */
    @Override
    public void updateStateWithFlavor(Long[] ids,Integer id) {
        for (int i = 0; i < ids.length; i++) {
            Dish byId = getById(ids[i]);
            byId.setStatus(id);
            this.updateById(byId);
        }
    }
}
